from datetime import datetime

from mongoengine import *
from werkzeug.security import (check_password_hash, generate_password_hash, )

from src.model import BaseDocument


class Admin(BaseDocument):
    username     = StringField(min_length=4, max_length=20, unique=True, required=True, )
    _password    = StringField(unique=True, required=True, )
    nickname     = StringField(min_length=2, max_length=10, null=True, )
    birthday     = DateTimeField(default=datetime.now)
    position     = StringField(null=True)
    git_address  = StringField(null=True)
    location     = StringField(null=True)
    skill        = ListField(DictField(), null=True)
    is_lock      = BooleanField(default=False)  # 是否被锁定
    account_type = IntField(default=0)  # 0 资产管理员，1 超级管理员，2 root管理员

    @property
    def password(self):
        return self._password

    @password.setter
    def password(self, raw_password):
        self._password = generate_password_hash(raw_password)

    def check_password(self, raw_password):
        return check_password_hash(self.password, raw_password)
